package com.zhang.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhang.mapper.IOrderInfoMapper;
import com.zhang.mapper.IOrderMapper;
import com.zhang.mapper.IShopMapper;
import com.zhang.pojo.OrderBean;
import com.zhang.pojo.OrderInfoBean;
import com.zhang.pojo.SaleBean;
import com.zhang.pojo.ShopBean;
import com.zhang.service.IOrderService;
import com.zhang.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private IOrderMapper orderMapper;
    @Autowired
    private IShopMapper shopMapper;
    @Autowired
    private IOrderInfoMapper orderInfoMapper;


    @Override
    public OrderBean findById(int orderId) {
        return orderMapper.findById(orderId);
    }

    @Override
    public void add(OrderBean orderBean,int userId) {
       orderMapper.add(orderBean,userId);
        List<ShopBean> byUserId = shopMapper.findByUserId(userId);
        for (ShopBean shopBean : byUserId) {
//            System.out.println("=========================================="+shopBean.getProductBean().getProductPrice());
            orderInfoMapper.add(new OrderInfoBean(shopBean.getProductBean().getProductId(),shopBean.getNum(),shopBean.getProductBean().getProductPrice()),orderBean.getOrderId());
        }
        shopMapper.delByUserId(userId);
    }

    @Override
    public PageInfo<OrderBean> findByItem(int pageNo,String status, LocalDate start, LocalDate end) {
        PageHelper.startPage(pageNo,4);
        PageInfo<OrderBean> pageInfo = PageInfo.of(orderMapper.findByItem(status, start, end));
        return pageInfo;
    }

    @Override
    public PageInfo<OrderBean> findByUserId(int pageNo, Integer userId) {
        PageHelper.startPage(pageNo,4);
        PageInfo<OrderBean> pageInfo = PageInfo.of(orderMapper.findByUserId(userId));
        return pageInfo;
    }

    @Override
    public void send(int id) {
        orderMapper.update(id,"发货中");
    }

    @Override
    public List<OrderBean> findDeBoy() {
        return orderMapper.findDeBoy();
    }

    @Override
    public void finish(int id) {
        orderMapper.update(id,"已完结");
    }

    @Override
    public List<SaleBean> findAll(int year) {
        return orderMapper.findAll(year);
    }
}
